unit StudentDataForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons,
  TntForms, TntButtons, TntStdCtrls, ImgList, TntExtCtrls,
  TntClasses;

type

  TLoginState = (lsNo, lsNoChecked, lsOK, lsFail);

  TStudentForm = class(TTntForm)
    PanelMain: TTntPanel;
    LabeledEditLogin: TTntEdit;
    TntLabel1: TTntLabel;
    TntLabel2: TTntLabel;
    LabeledEditPassword: TTntEdit;
    LabeledEditName: TTntEdit;
    TntLabel3: TTntLabel;
    TntLabel4: TTntLabel;
    LabeledEditSurname: TTntEdit;
    ComboBoxGroups: TTntComboBox;
    TntLabel5: TTntLabel;
    ImageList: TImageList;
    ButtonCheckLogin: TTntImage;
    PanelBottom: TTntPanel;
    BitBtnOk: TTntBitBtn;
    BitBtnCancel: TTntBitBtn;
    LabelError: TTntLabel;
    procedure TntFormShow(Sender: TObject);
    procedure LabeledEditLoginChange(Sender: TObject);
    procedure LabeledEditSurnameChange(Sender: TObject);
    procedure LabeledEditLoginKeyPress(Sender: TObject; var Key: Char);
    procedure ButtonCheckLoginClick(Sender: TObject);
    procedure BitBtnOkClick(Sender: TObject);
    procedure LabeledEditPasswordChange(Sender: TObject);
  private
    fLoginsChangManualy: Boolean;
  private
    procedure SetLoginState(aState: TLoginState);
    procedure CheckValues;
    procedure SetPicture(aButton: TTntImage; aPictureNumber: Integer);
    function CheckLogin(aLogin: WideString): Boolean;
    function IsDataChanged: Boolean;
  public
    Host, Login, Password: string;
    StudentSurname, StudentName, StudentLogin, StudentPassword, StudentGroup: WideString;
    NewStudentSurname, NewStudentName, NewStudentLogin, NewStudentPassword, NewStudentGroup: WideString;
    Groups: TTntStringList;
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;  
  end;

implementation

uses
  uInterfaces,
  uRequestImpl,
  uSplashImpl,
  uUtils,
  uTexts;

{$R *.dfm}

{ TStudentForm }

function TStudentForm.CheckLogin(aLogin: WideString): Boolean;
var
  request: IgtRequest;
  response: IgtResponse;
  splash: IgtSplash;
begin
  splash := NewSplash(Self);
  splash.ShowSplash(cCheckLoginUnical);
  try
    request := NewRequest(Host, Login, Password);
    request.Param['Student-login'] := aLogin;
    response := request.ExecSync('GETSTUD');
    Result := response.Code = 301;
  finally
    splash.HideSplash;
  end;
end;

procedure TStudentForm.CheckValues;
var
  ok: boolean;
  error: WideString;
begin
  ok := True;

  if LabeledEditLogin.Text = '' then
    begin
    error := cEmptyLogin;
    ok := False;
    end;

  if ok and (LabeledEditPassword.Text = '') then
    begin
    error := cEmptyPassword;
    ok := False;
    end;

  if ok and not IsShortText(LabeledEditLogin.Text) then
    begin
    error := cLongLogin;
    ok := False;
    end;

  if ok and not IsShortText(LabeledEditLogin.Text) then
    begin
    error := cLongPassword;
    ok := False;
    end;

  if ok and not IsShortText(LabeledEditName.Text) then
    begin
    error := cLongName;
    ok := False;
    end;

  if ok and not IsShortText(LabeledEditName.Text) then
    begin
    error := cLongSurname;
    ok := False;
    end;

  BitBtnOk.Enabled := ok;
  if not ok then
    LabelError.Caption := error
  else
    LabelError.Caption := '';
end;

constructor TStudentForm.Create(AOwner: TComponent);
begin
  inherited;
  Groups := TTntStringList.Create;
end;

destructor TStudentForm.Destroy;
begin
   FreeAndNil(Groups);
  inherited;
end;

function TStudentForm.IsDataChanged: Boolean;
begin
  if StudentLogin <> '' then
    Result := (StudentLogin <> LabeledEditLogin.Text) or
      (StudentName <> LabeledEditName.Text) or
      (StudentSurname <> LabeledEditSurname.Text) or
      (StudentPassword <> LabeledEditPassword.Text) or
      (StudentGroup <> ComboBoxGroups.Text)
  else
    Result := ('' <> LabeledEditLogin.Text) or
      ('' <> LabeledEditName.Text) or
      ('' <> LabeledEditSurname.Text) or
      ('' <> LabeledEditPassword.Text) or
      (cNoGroups <> ComboBoxGroups.Text);
end;

procedure TStudentForm.LabeledEditLoginChange(Sender: TObject);
begin
  if Trim(LabeledEditLogin.Text) <> '' then
    SetLoginState(lsNoChecked)
  else
    SetLoginState(lsNo);
  CheckValues;
end;

procedure TStudentForm.LabeledEditLoginKeyPress(Sender: TObject; var Key: Char);
begin
  fLoginsChangManualy := Trim(LabeledEditLogin.Text) <> '';
end;

procedure TStudentForm.LabeledEditPasswordChange(Sender: TObject);
begin
  CheckValues;
end;

procedure TStudentForm.LabeledEditSurnameChange(Sender: TObject);
begin
  if not fLoginsChangManualy then
    begin
    LabeledEditLogin.Text := TranslitKir2Lat(LabeledEditSurname.Text);
    if LabeledEditName.Text <> '' then
      LabeledEditLogin.Text := LabeledEditLogin.Text + '_' + TranslitKir2Lat(LabeledEditName.Text);
    end;
end;

procedure TStudentForm.SetLoginState(aState: TLoginState);
begin
  case aState of
    lsNo:
      begin
      SetPicture(ButtonCheckLogin, 0);
      ButtonCheckLogin.Hint := '';
      ButtonCheckLogin.Enabled := False;
      end;
    lsNoChecked:
      begin
      SetPicture(ButtonCheckLogin, 1);
      ButtonCheckLogin.Hint := cCheckLoginAvailable;
      ButtonCheckLogin.Enabled := True;
      end;
    lsOK:
      begin
      SetPicture(ButtonCheckLogin, 2);
      ButtonCheckLogin.Hint := cLoginUnical;
      ButtonCheckLogin.Enabled := False;
      end;
    lsFail:
      begin
      SetPicture(ButtonCheckLogin, 3);
      ButtonCheckLogin.Hint := cStudentAlreadyExists;
      ButtonCheckLogin.Enabled := False;
      end;
  end;
end;

procedure TStudentForm.SetPicture(aButton: TTntImage;
  aPictureNumber: Integer);
var
  btm: TBitmap;
begin
btm := TBitmap.Create;
try
  ImageList.GetBitmap(aPictureNumber, btm);
  btm.TransparentColor := clWhite;
  aButton.Picture.Bitmap := btm;
  aButton.Transparent := True;
finally
  btm.Free;
end;
end;

procedure TStudentForm.TntFormShow(Sender: TObject);
begin
  LabeledEditLogin.Text := StudentLogin;
  if StudentLogin <> '' then
    begin
    SetLoginState(lsOK);
    fLoginsChangManualy := True;
    end
  else
    begin
    SetLoginState(lsNo);
    fLoginsChangManualy := False;
    end;
  LabeledEditPassword.Text := StudentPassword;
  LabeledEditName.Text := StudentName;
  LabeledEditSurname.Text := StudentSurname;
  ComboBoxGroups.Items.AddStrings(Groups);
  ComboBoxGroups.Items.Insert(0, cNoGroups);
  ComboBoxGroups.ItemIndex := ComboBoxGroups.Items.IndexOf(StudentGroup);
  if ComboBoxGroups.ItemIndex = -1 then
    ComboBoxGroups.ItemIndex := 0;
  if Groups.Count = 0 then
    ComboBoxGroups.Enabled := False;
  CheckValues;
end;

procedure TStudentForm.BitBtnOkClick(Sender: TObject);
var
  request: IgtRequest;
  response: IgtResponse;
  splash: IgtSplash;
begin
  if not IsDataChanged then
    begin
    ModalResult := mrCancel;
    Exit;
    end;

  splash := NewSplash(Self);
  splash.ShowSplash(cAddStudent);
  try
    request := NewRequest(Host, Login, Password);
    if StudentLogin = '' then
      begin
      request.Param['Student-login'] := LabeledEditLogin.Text;
      request.Param['Student-password'] := LabeledEditPassword.Text;
      request.Param['Student-name'] := LabeledEditName.Text;
      request.Param['Student-surname'] := LabeledEditSurname.Text;
      if ComboBoxGroups.ItemIndex <> 0 then
        request.Param['Group'] := ComboBoxGroups.Text;
      response := request.ExecSync('ADDSTUD');
      end
    else
      begin
      request.Param['Student-login'] := StudentLogin;
      request.Param['Student-newlogin'] := LabeledEditLogin.Text;
      request.Param['Student-newpassword'] := LabeledEditPassword.Text;
      request.Param['Student-newname'] := LabeledEditName.Text;
      request.Param['Student-newsurname'] := LabeledEditSurname.Text;
      if ComboBoxGroups.ItemIndex <> 0 then
        request.Param['Student-newgroup'] := ComboBoxGroups.Text;
      response := request.ExecSync('EDITSTUD');
      end;
  finally
    splash.HideSplash;
  end;
  case response.Code of
    200: ModalResult := mrOk;
    302: begin
         ShowWarningMessage(cStudentAlreadyExists, Self);
         ModalResult := mrNone;
         Exit;
         end
  else
    begin
    ShowWarningMessage(response.MessageText);
    ModalResult := mrNone;
    Exit;
    end;
  end;
  NewStudentLogin := LabeledEditLogin.Text;
  NewStudentPassword := LabeledEditPassword.Text;
  NewStudentName := LabeledEditName.Text;
  NewStudentSurname := LabeledEditSurname.Text;
  if ComboBoxGroups.ItemIndex <> 0 then
    NewStudentGroup := ComboBoxGroups.Text
  else
    NewStudentGroup := '';
end;

procedure TStudentForm.ButtonCheckLoginClick(Sender: TObject);
begin
  if CheckLogin(LabeledEditLogin.Text) then
    SetLoginState(lsOK)
  else
    SetLoginState(lsFail);
end;

end.
